package com.dj.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dj.dormitory.commonutils.Result;
import com.dj.dormitory.entity.*;
import com.dj.dormitory.entity.vo.BedVo;
import com.dj.dormitory.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DJ同学
 * @since 2021-01-24
 */
@RestController
@RequestMapping("/bed")
public class BedController {

    @Autowired
    private BedService bedService;

    @Autowired
    private DormitoryStudentService dormitoryStudentService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrgService orgService;

    @PostMapping("getList")
    public Result getList(@RequestBody Bed bed){
        QueryWrapper<Bed> wrapper = new QueryWrapper<>();
        wrapper.eq("dormitory_id",bed.getDormitoryId());
        List<Bed> beds = bedService.list(wrapper);

        List<BedVo> list = new ArrayList<>();
        for (int i = 0; i < beds.size(); i++) {
            BedVo bedVo = new BedVo();
            BeanUtils.copyProperties(beds.get(i),bedVo);
            QueryWrapper<DormitoryStudent> dormitoryStudentQueryWrapper = new QueryWrapper<>();
            dormitoryStudentQueryWrapper.eq("bed_id",beds.get(i).getId());
            List<DormitoryStudent> dormitoryStudent = dormitoryStudentService.list(dormitoryStudentQueryWrapper);
            if(dormitoryStudent==null || dormitoryStudent.size()==0){
                bedVo.setClassName("暂无");
                bedVo.setGradeId(-1);
                bedVo.setStudentName("暂无");
            }else{
                Student student = studentService.getById(dormitoryStudent.get(0).getStudentId());
                User user = userService.getById(student.getUserId());
                Org org = orgService.getById(student.getClassId());
                bedVo.setClassName(org.getName());
                bedVo.setGradeId(student.getGradeId());
                bedVo.setStudentName(user.getName());
            }
            list.add(bedVo);
        }
        return Result.ok().data("beds",list);
    }
}

